package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guoq.mapper.ItemCatMapper;
import com.guoq.pojo.ItemCat;
import com.guoq.pojo.ItemCatExample;
import com.guoq.pojo.ItemCatExample.Criteria;
import com.guoq.pojo.Result;
import com.guoq.service.IItemCatService;

@Service("itemCatService")
public class ItemCatServiceImpl implements IItemCatService {

	@Resource
	private ItemCatMapper itemCatMapper;

	@Override
	public ItemCat getItemCatById(int id) {
		// TODO Auto-generated method stub
		return this.itemCatMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<ItemCat> getItemCatList() {
		// TODO Auto-generated method stub
		ItemCatExample example = new ItemCatExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(0);//正常
		example.setOrderByClause("id");
		List<ItemCat> list = itemCatMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<ItemCat> selectByExample(ItemCatExample example) {
		// TODO Auto-generated method stub
		List<ItemCat> list = itemCatMapper.selectByExample(example);
		return list;
	}

	@Override
	public ItemCat selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		ItemCat ItemCat = itemCatMapper.selectByPrimaryKey(id);
		return ItemCat;
	}

	@Override
	public int countByExample(ItemCatExample example) {
		// TODO Auto-generated method stub
		int count = itemCatMapper.countByExample(example);
		return count;
	}

	@Override
	public Result createItemCat(ItemCat ItemCat) throws Exception {

		Result result = this.insertItemCat(ItemCat);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 添加商品描述
	 *<P>开发者:郭强
	 *<P>方法名: insertItemDesc
	 *<P>描述: 
	 *@param itemId
	 *@param desc
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	private Result insertItemCat(ItemCat ItemCat){
		
		ItemCat.setStatus(0);
		ItemCat.setCreatetime(new Date());
		ItemCat.setUpdatetime(new Date());
		itemCatMapper.insertSelective(ItemCat);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(ItemCat record) throws Exception {

		Result result = null;
		
		record.setStatus(2);
		record.setUpdatetime(new Date());
		int num = itemCatMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result updateItemCat(ItemCat record) throws Exception {

		Result result = null;
		int num = itemCatMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 根据pid查询下级类目列表
	 */
	@Override
	public List<ItemCat> selectItemCatByPid(int pid) {

		ItemCatExample example = new ItemCatExample();
		Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(pid);
		criteria.andStatusEqualTo(0);//正常状态
		example.setOrderByClause("sort");
		List<ItemCat> list = this.itemCatMapper.selectByExample(example);
		return list;
	}
}
